package admin

import (
	"game-server/app/common/request"
	"game-server/app/common/response"
	adminservices "game-server/app/services/admin"

	"github.com/gin-gonic/gin"
)

// SysMenuAllList 获取所有菜单列表
// @Summary 获取所有菜单列表
// @Description 获取系统中所有菜单的树形结构
// @Tags 系统菜单
// @Accept json
// @Produce json
// @Router /gameServer/admin/menu/alllist [get]
func SysMenuAllList(c *gin.Context) {
	menus, err := adminservices.SysMenuService.GetAllMenus()
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, menus)
}

// SysMenuDelete 删除菜单
// @Summary 删除菜单
// @Description 根据ID删除菜单节点
// @Tags 系统菜单
// @Accept json
// @Produce json
// @Param id path int true "菜单ID"
// @Router /gameServer/admin/menu/{id} [delete]
func SysMenuDelete(c *gin.Context) {
	id := c.Param("id")
	if err := adminservices.SysMenuService.DeleteMenu(id); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, nil)
}

// SysMenuGet 获取菜单详情
// @Summary 获取菜单详情
// @Description 根据ID获取菜单节点详细信息
// @Tags 系统菜单
// @Accept json
// @Produce json
// @Param id path int true "菜单ID"
// @Router /gameServer/admin/menu/{id} [get]
func SysMenuGet(c *gin.Context) {
	id := c.Param("id")
	menu, err := adminservices.SysMenuService.GetMenuById(id)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, menu)
}

// SysMenuList 菜单分页列表
// @Summary 菜单分页列表
// @Description 带条件分页查询菜单信息
// @Tags 系统菜单
// @Accept json
// @Produce json
// @Param name query string false "菜单名称"
// @Param path query string false "路由路径"
// @Param status query int false "状态"
// @Param parent_id query int false "父级ID"
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Router /gameServer/admin/menu/list [get]
func SysMenuList(c *gin.Context) {
	var form request.SysMenuListRequest
	if err := c.ShouldBindQuery(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	list, total, err := adminservices.SysMenuService.GetMenuList(form)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, gin.H{
		"list":  list,
		"total": total,
	})
}

// SysMenuAdd 添加菜单
// @Summary 添加菜单
// @Description 添加新的菜单节点
// @Tags 系统菜单
// @Accept json
// @Produce json
// @Param data body request.SysMenuAddRequest true "菜单信息"
// @Router /gameServer/admin/menu [post]
func SysMenuAdd(c *gin.Context) {
	var form request.SysMenuAddRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	menu, err := adminservices.SysMenuService.CreateMenu(form)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, menu)
}

// SysMenuUpdate 更新菜单
// @Summary 更新菜单
// @Description 根据ID更新菜单节点信息
// @Tags 系统菜单
// @Accept json
// @Produce json
// @Param id path int true "菜单ID"
// @Param data body request.SysMenuUpdateRequest true "菜单信息"
// @Router /gameServer/admin/menu/{id} [put]
func SysMenuUpdate(c *gin.Context) {
	id := c.Param("id")
	var form request.SysMenuUpdateRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	if err := adminservices.SysMenuService.UpdateMenu(id, form); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, nil)
}

// SysMenuRoleGet 获取角色的菜单权限
// @Summary 获取角色的菜单权限
// @Description 根据角色ID获取已分配的菜单权限
// @Tags 系统菜单
// @Accept json
// @Produce json
// @Param roleId path int true "角色ID"
// @Router /gameServer/admin/menu/role/{roleId} [get]
func SysMenuRoleGet(c *gin.Context) {
	roleId := c.Param("roleId")
	menus, err := adminservices.SysMenuService.GetRoleMenus(roleId)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, menus)
}

// SysMenuAssignMenu 分配菜单权限
// @Summary 分配菜单权限
// @Description 为角色分配菜单权限
// @Tags 系统菜单
// @Accept json
// @Produce json
// @Param data body request.SysMenuAssignRequest true "权限分配信息"
// @Router /gameServer/admin/menu/assignMenu [put]
func SysMenuAssignMenu(c *gin.Context) {
	var form request.SysMenuAssignRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	if err := adminservices.SysMenuService.AssignMenus(form); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, nil)
}
